#include "vertex.h"
#include "graph.h"
#include <QPainter>

Vertex::Vertex(Graph *parent)
	: QGraphicsItem()
	, m_parent(parent)
{
	setZValue(1);
	setAcceptsHoverEvents(true);
}

Vertex::Vertex(Graph *parent, const QString &name)
	: QGraphicsItem()
	, m_parent(parent)
{
	setZValue(1);
	setAcceptsHoverEvents(true);
	setName(name);
	setPos((qrand() % 980) - 490, (qrand() % 580) - 290);
}

void Vertex::setParentGraph(Graph *g)
{
	m_parent = g;
}

Graph *Vertex::parentGraph() const
{
	return m_parent;
}

QRectF Vertex::boundingRect() const
{
	return QRectF(-5.5, -5.5, 11, 11);
}

void Vertex::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(widget)
	Q_UNUSED(option)
	painter->fillPath(shape(), Qt::black);
}

QPainterPath Vertex::shape() const
{
	QPainterPath path;
	path.addEllipse(boundingRect().adjusted(0.5,0.5,-0.5,-0.5));
	return path;
}

QList<Vertex *> Vertex::neighbours() const
{
	if (!m_parent)
		return QList<Vertex *>();
	return m_parent->vertices(this);
}

QString Vertex::name() const
{
	return m_name;
}

void Vertex::setName(const QString &name)
{
	m_name = name;
}
